package org.cerberus.platform.internal.server.jetty;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.mortbay.io.Connection;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.RetryRequest;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.SessionIdManager;
import org.mortbay.jetty.handler.AbstractHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.nio.SelectChannelConnector.RetryContinuation;
import org.mortbay.jetty.servlet.DefaultServlet;
import org.mortbay.jetty.servlet.HashSessionIdManager;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.servlet.ServletMapping;
import org.mortbay.util.ajax.Continuation;
import org.mortbay.util.ajax.ContinuationSupport;
import org.mortbay.util.ajax.WaitingContinuation;

public class ContinuationTest {

	private static Object lock = new Object();
	
	private static class MyServlet extends HttpServlet {
		
		private Continuation continuation;
		
		private Continuation retryContinuation;
		
		@Override
		protected void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			if (retryContinuation == null) {
				retryContinuation = new RetryContinuation();
			}
			response.setContentType("text/plain");
			response.getOutputStream().write("hello world".getBytes());
			 request.setAttribute("org.mortbay.jetty.ajax.Continuation", retryContinuation);
			 String type = request.getParameter("type");
			 
			 if (type.equals("wakup")) {
				 System.out.println("call resume "+Thread.currentThread().getName());
				 continuation.resume();
			 } else if (type.equals("sleep")) {
			        // Is there any chat events ready to send?
		            // No - so prepare a continuation
	            continuation = ContinuationSupport.getContinuation(request, lock);
	            System.out.println("suspend "+Thread.currentThread().getName());
	            // wait for an event or timeout
	            
	            continuation.suspend(Long.MAX_VALUE);
	            System.out.println("resumed "+Thread.currentThread().getName());
			 }
			
		}
	}
	
	public static void main(String[] args) throws Exception {
		Server jetty = new Server(8001);
		SelectChannelConnector connector = new SelectChannelConnector();
		connector.setPort(8001);
		jetty.setConnectors(new Connector[]{connector});
		ServletHandler handler = new ServletHandler();
		ServletHolder holder = new ServletHolder(new MyServlet());
		holder.setName("test");
		ServletMapping mapping = new ServletMapping();
		mapping.setPathSpec("/test");
		mapping.setServletName("test");
		handler.addServlet(holder);
		handler.addServletMapping(mapping);
		
		jetty.addHandler(handler);
		jetty.setSessionIdManager(new HashSessionIdManager());
		jetty.start();
		
		URL sleepUrl = new URL("http://localhost:8001/test?type=sleep");
		URL wakeupUrl = new URL("http://localhost:8001/test?type=wakeup");
		
		Thread.sleep(1000);
		call(sleepUrl);
		
		Thread.sleep(1000);
		call(wakeupUrl);
		
		System.exit(0);
	}
	
	private static void call(URL url) throws IOException {
		System.out.println("Call "+url);
		URLConnection connection = url.openConnection();
		connection.setConnectTimeout(0);
		connection.setUseCaches(false);
		connection.connect();
		connection.getContent();
	}
	
	
}
